package boogui.member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;
import boogui.db.MemberSQL;

public class MemberLoginDAO implements IMemberLoginDAO {
	IBooguiDAO bdao = null;
	MemberSQL msql = null;

	public MemberLoginDAO() {
		bdao = new BooguiDAO();
		msql = new MemberSQL();
	}

	// 로그인
	@Override
	public String selectPwd(String id) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String result = "";

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getSelectPwdSQL());
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getString("mempwd");
			}
		} catch (Exception ex) {
			//ex.printStackTrace();
			JOptionPane.showMessageDialog(null, ex.getMessage());
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}
	
	@Override
	public boolean loginWho(String id) {
		/*Connection conn = null;
		PreparedStatement pstmt = null;		*/
		boolean result = false;
/*
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getLoginSQL());
			pstmt.setString(1, id);
			pstmt.executeUpdate();
			
			result = true;			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, null);
		}*/	//131209
		return result;
	}
	
	@Override
	public List<MemberVO> loginView(UserVO u) {//////////////////////////////////////////
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MemberVO> result = new ArrayList<>();

		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(msql.getLoginViewSQL());
			pstmt.setString(1, u.getUserid());
			rs = pstmt.executeQuery();
			while(rs.next()) {
				MemberVO m = new MemberVO();
				m.setMemid(rs.getString("memid")) ;
				m.setMemname(rs.getString("memname"));
				m.setMembirth(rs.getString("membirth"));
				m.setMemgenre(rs.getString("memgenre"));
				m.setMemhp(rs.getString("memhp"));
				m.setMememail(rs.getString("mememail"));
				result.add(m);
				
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}


}
